package com.iotproject.studyservice.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyWord;
import com.iotproject.studyservice.entity.vo.admin.WordQuery;
import com.iotproject.studyservice.service.StudyGradeService;
import com.iotproject.studyservice.service.StudyWordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//后台管理->单词管理
@RestController
@RequestMapping("admin/studyservice/studyword/")
public class AdminStudyWordController {

    @Autowired
    StudyWordService studyWordService;

    @Autowired
    StudyGradeService studyGradeService;

    //条件查询带分页的方法
    @PostMapping("pageWordCondition/{current}/{limit}")
    public R pageWordCondition(@PathVariable("current") long current,
                               @PathVariable("limit") long limit,
                               @RequestBody(required = false) WordQuery wordQuery) {

        Page<StudyWord> wordPage = new Page<>(current, limit);
        QueryWrapper<StudyWord> wordQueryWrapper = new QueryWrapper<>();

        String name = wordQuery.getName();
        String grade = wordQuery.getGrade();
        if (!StringUtils.isEmpty(name)) {
            wordQueryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(grade)) {
            wordQueryWrapper.eq("grade", grade);
        }
//        wordQueryWrapper.orderByDesc("gmt_create");
        studyWordService.page(wordPage, wordQueryWrapper);
        long total = wordPage.getTotal();
        List<StudyWord> records = wordPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    //删除单词方法
    @DeleteMapping("deleteWord/{id}")
    @ApiOperation(value = "根据id删除单词")
    public R deleteWordById(@PathVariable("id") String id) {
        boolean result = studyWordService.removeById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id获取单词信息
    @GetMapping("getWordMsg/{id}")
    public R getWordMsgById(@PathVariable("id") String id) {
        StudyWord word = studyWordService.getById(id);
        return R.ok().data("word", word);
    }

    //修改单词信息
    @PostMapping("updateWordMsg")
    public R updateWordMsg(@RequestBody StudyWord studyWord) {
        //studyWord 包含id
        boolean updateWord = studyWordService.updateById(studyWord);
        if (updateWord) {
            return R.ok();
        }
        return R.error();
    }

    //传入Excel文件，将内容存储到数据库
    @PostMapping("addWord")
    public R upload(MultipartFile file) {
        studyWordService.importStudyWordData(file, studyWordService, studyGradeService);
        return R.ok();
    }

}
